package javaweb_work01.main.Servlet;

import javaweb_work01.main.Utils.PageContrl;
import javaweb_work01.main.Entity.Students;
import javaweb_work01.main.Service.CountService;
import javaweb_work01.main.Service.ShowService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用于控制分页显示的Servlet
 */
@WebServlet(name = "ShowServlet",urlPatterns = "/show")
public class ShowServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到主页面分页按钮设置的start属性值，即每一页的第一条数据的编号
        int start = Integer.parseInt(request.getParameter("start"));
        //得到学员信息总数
        CountService countService = new CountService();
        int count = countService.countStudentsService();
        //用得到的学员信息总数和start的属性值构造新的PageContrl对象
        ShowService showService = new ShowService();
        PageContrl pageContrl = new PageContrl(start,10,count);
        //将新构造的PageContrl对象作为参数传入Service,调用Dao层方法,得到该页的学员数据集合
        List<Students> students = showService.showStudentsService(pageContrl);

        //将得到的PageContrl对象和Students对象放入session中实现共享
        request.getSession().setAttribute("pageContrl",pageContrl);
        request.getSession().setAttribute("showStudents",students);
        //跳转到功能页面
        response.sendRedirect("main.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
